//==================商家端======================

const dataKit = require( "./util/dataKit")
const dateKit = require( "./util/dateKit")
const businessService = require( "./util/businessService");
const util = require("./util/util");

/**
 * 登录（保存token）
 */
exports.login = async (context) => {
    const {userNick} = context.data;
    const {accessToken,openId} = context;
    if(!userNick){
        return {code:-1,msg:"userNick是必须的"};
    }
    if(!accessToken){
        return {code:-1,msg:"请使用真机调试",data:"accessToken是必须的"};
    }
    
    let merchant = await businessService.getMerchantByOpenId(context,openId);
    if(!merchant){
        merchant = {
            openId:openId,
            createTime:dateKit.getCurrentDateTime()
        };
    }
    
    merchant.loginTime =  dateKit.getCurrentDateTime();
    merchant.userNick = userNick;
    merchant.accessToken = accessToken;
    const result = await dataKit.insertOrUpdate(context,"merchant_info",merchant);
    return {code:0,msg:"登录成功"};
};


/**
 * 获取完成上传作品人数
 */
exports.getUploadWorkPeopleNum = async (context) => {
  let sys_setting = await dataKit.findOne(context,"sys_setting",{key:"sys_setting"});
  return {code:0,msg:"获取成功",data:sys_setting.uploadWorkPeopleNum};
}

/**
 * 设置完成上传作品人数
 * @param {*} context 
 */
exports.setUploadWorkPeopleNum = async (context) => {
  const {uploadWorkPeopleNum} = context.data;
  if(!uploadWorkPeopleNum){
    return {code:-1,msg:"缺少uploadWorkPeopleNum参数"};
  }
  
  let sys_setting = await dataKit.findOne(context,"sys_setting",{key:"sys_setting"});
  sys_setting.uploadWorkPeopleNum = uploadWorkPeopleNum;
  await dataKit.updateBy_id(context,"sys_setting",sys_setting);
  return {code:0,msg:"修改成功",data:sys_setting.uploadWorkPeopleNum};
}

/**
 * 获取作品列表
 * @param {*} context 
 */
exports.getWorkList = async (context) => {
  let {status,userNick,pageNum,pageSize} = context.data;
  if(!pageNum){
    pageNum=1;
  }
  if(!pageSize){
    pageSize=15;
  }

  let filter = {status};
  if(userNick){
    filter.userNick = {$regex:userNick};
  }
  let page = await dataKit.findPage(context,"work",filter,{createTime:-1},pageNum,pageSize);

  return {code:0,msg:"获取成功",data:page};
}


/**
 * 审核作品
 * @param {*} context 
 */
exports.approve = async (context) => {
  const {openId,userNick} = context;
  const {_id,status} = context.data;
  let check = util.checkParamsNull(context,"_id,status");
  if(check){
    return {code:-1,msg:"缺少status参数"};
  }
  
  let work = await dataKit.findOne(context,"work",{_id});
  work.status = status;
  work.approverOpenId=openId;
  work.approverUserNick=userNick;
  work.approveTime = dateKit.getCurrentDateTime();
  await dataKit.updateBy_id(context,"work",work);

  return {code:0,msg:"审核成功"};
}


/**
 * 修改票数
 * @param {*} context 
 */
exports.setVoteNum = async (context) => {
  const {openId,userNick} = context;
  const {_id,voteNum} = context.data;
  let check = util.checkParamsNull(context,"_id,voteNum");
  if(check){
    return {code:-1,msg:"缺少status参数"};
  }
  
  let work = await dataKit.findOne(context,"work",{_id});
  work.voteNum = voteNum;
  await dataKit.updateBy_id(context,"work",work);
  
  return {code:0,msg:"票数修改成功",voteNum};
}
